Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

120
Vistas
¿Qué causa el error "Expresión de argumento esperada" en esta aplicación Angular 13?

Estoy trabajando en una aplicación de comercio electrónico cuyo front-end está hecho en Angular 13.

El siguiente código está destinado a sumar los precios de los artículos en el carrito:

 import { Component, OnInit } from '@angular/core'; @Component({ selector: '.app-top-cart', templateUrl: './top-cart.component.html', styleUrls: ['./top-cart.component.css'] }) export class TopCartComponent implements OnInit { cartItems: any = [ { id: 1, title: "iPhone 9", description: "An apple mobile which is nothing like apple", price: 549, discountPercentage: 12.96, rating: 4.69, stock: 94, brand: "Apple", category: "smartphones", thumbnail: "https://dummyjson.com/image/i/products/1/thumbnail.jpg", images: [ "https://dummyjson.com/image/i/products/1/1.jpg", "https://dummyjson.com/image/i/products/1/2.jpg", ] }, { id: 2, title: "iPhone X", description: "SIM-Free, Model A19211 6.5-inch Super Retina HD display with OLED technology A12 Bionic chip with ...", price: 899, discountPercentage: 17.94, rating: 4.44, stock: 34, brand: "Apple", category: "smartphones", thumbnail: "https://dummyjson.com/image/i/products/2/thumbnail.jpg", images: [ "https://dummyjson.com/image/i/products/2/1.jpg", "https://dummyjson.com/image/i/products/2/2.jpg", ] } ]; constructor() { } totalPrice: number = 0; doTotalPrice(){ let total = 0, this.cartItems.forEach((item: { price: number, quantity: number; }) => { total += item.price * item.quantity }), this.totalPrice = total, } ngOnInit(): void { this.doTotalPrice(); } }

El problema

El código anterior no se compila. Da la Argument expression expected en la línea 54, donde se cierra el método doTotalPrice .

¿Dónde está mi error?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

doTotalPrice(){ let total = 0, this.cartItems.forEach((item: { price: number, quantity: number; }) => { total += item.price * item.quantity }, this.totalPrice = total, }

No cierra su función y usa comas en lugar de punto y coma.

 doTotalPrice(){ let total = 0, this.cartItems.forEach((item: { price: number, quantity: number; }) => { total += item.price * item.quantity }); this.totalPrice = total; }

Bonificación, código más limpio:

 this.totalPrice = this.cartItems.reduce( (p, { price, quantity }) => p + price * quantity, 0 );
about 4 years ago · Juan Pablo Isaza Denunciar

0

debe eliminar la coma después de forEach();

 yourArray.forEach(element=>{ //do something });
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda